package com.zmn.mcc.services.impl.role;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.common.dto.role.RoleDIO;
import com.zmn.mcc.common.dto.role.RoleDRO;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.role.McRoleQuery;
import com.zmn.mcc.model.entity.role.RoleIdAndDayLimit;
import com.zmn.mcc.persistence.interfaces.role.RoleDao;
import com.zmn.mcc.services.interfaces.role.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 14:50
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDao roleDao;


    @Override
    public McRole findByKey(Integer roleId) {
        return roleDao.findByKey(roleId);
    }

    @Override
    public List<McRole> listPageByQuery(McRoleQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : roleDao.listPageByQuery(query);
    }

    @Override
    public List<McRole> listByType(Integer type) {
        return this.listByType(type, null);
    }

    @Override
    public List<McRole> listByType(Integer type, Integer status) {
        McRoleQuery query = new McRoleQuery();
        query.setType(type);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McRole> listByDeptId(Integer deptId) {
        return this.listByDeptId(deptId, null);
    }

    @Override
    public List<McRole> listByDeptId(Integer deptId, Integer status) {
        McRoleQuery query = new McRoleQuery();
        query.setDeptId(deptId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McRole> listByDeptIds(List<Integer> deptIds) {
        return this.listByDeptIds(deptIds, null);
    }

    @Override
    public List<McRole> listByDeptIds(List<Integer> deptIds, Integer status) {
        McRoleQuery query = new McRoleQuery();
        query.setDeptIds(deptIds);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McRole> listByQuery(McRoleQuery query) {
        return roleDao.listByQuery(query);
    }

    @Override
    public List<McRole> listByIds(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return Collections.emptyList();
        }
        return roleDao.listByIds(ids);
    }

    @Override
    public List<Integer> listRoleIdByQuery(McRoleQuery query) {
        return roleDao.listRoleIdByQuery(query);
    }

    /**
     * 描述: 根据角色id集合、状态查询查询前溯天数
     *
     * @param status 状态 1:停用；2：启用
     * @param roleIdList 员工id集合
     * @return List<RoleIdAndDayLimit>
     * @author xujie
     * @since 2021/05/28
     **/
    @Override
    public List<RoleIdAndDayLimit> listRoleIdAndDayLimitByRoleIdListAndStatus(List<Integer> roleIdList, Integer status) {

        if (roleIdList == null || roleIdList.size() == 0) {
            return Collections.emptyList();
        }
        return roleDao.listRoleIdAndDayLimitByRoleIdListAndStatus(roleIdList, status);
    }

    @Override
    public Integer countByQuery(McRoleQuery query) {
        return roleDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McRole role) {
        if (role.getUpdateTime() == null) {
            role.setUpdateTime(DateUtil.getNow());
        }
        return roleDao.updateByKey(role);
    }

    @Override
    public Integer updateStatus(Integer roleId, Integer status, String updater) {
        McRole role = new McRole();
        role.setRoleId(roleId);
        role.setStatus(status);
        role.setUpdater(updater);
        return this.updateStatus(role);
    }

    @Override
    public Integer updateStatus(McRole role) {
        if (role.getUpdateTime() == null) {
            role.setUpdateTime(DateUtil.getNow());
        }
        return roleDao.updateByKey(role);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> roleIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(roleIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return roleDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer roleId, Integer sort) {
        McRole role = new McRole();
        role.setRoleId(roleId);
        role.setSort(sort);
        return this.updateSort(role);
    }

    @Override
    public Integer updateSort(McRole role) {
        return roleDao.updateSort(role);
    }


    @Override
    public Integer insert(McRole role) {
        role.setSort(100);
        role.setStatus(GlobalDict.STATUS_ENABLE);
        if (role.getUpdateTime() == null) {
            role.setUpdateTime(DateUtil.getNow());
        }
        role.setCreater(role.getUpdater());
        role.setCreateTime(role.getUpdateTime());
        return roleDao.insert(role);
    }


    @Override
    public Integer deleteByKey(Integer roleId) {
        return roleDao.deleteByKey(roleId);
    }
}
